package cn.tedu.property_management_system.utils;

import cn.tedu.property_management_system.ex.ServiceException;

public class JsonResult<T> {
    private Integer state;
    private String message;
    private T data;

    private JsonResult() {
    }

    public static JsonResult<Void> fail(ServiceException e){
        return JsonResult.fail(e.getMessage(),e.getState());
    }

    public static JsonResult<Void> fail(String message,State state){
        JsonResult<Void> jsonResult = new JsonResult<>();
        jsonResult.setState(state.getValue());
        jsonResult.setMessage(message);
        return jsonResult;
    }

    public static JsonResult<Void> OK(){
        return OK(null);
    }

    public static<T> JsonResult OK(T data){
        JsonResult<T> jsonResult = new JsonResult<>();
        jsonResult.setData(data);
        jsonResult.setState(State.OK.value);
        return jsonResult;
    }

    public enum State{
        OK(200),
        BAD_REQUEST(400),
        ERR_USERNAME(401),
        ERR_PASSWORD(402),
        /**
         * 访问禁止返回403
         */
        FORBIDDEN(403),
        NO_SUCH_RESOURCE(404),
        ERR_FILE_UPLOAD_EXCEPTION(406),
        UNAUTHORIZED(407),
        UNKNOW_EXCEPTION(500);

        Integer value;

        State (Integer value){
            this.value=value;
        }

        public Integer getValue() {
            return value;
        }
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
